﻿using MettingOrder.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace MettingOrder.Controllers
{
    public class MettingRoomController : BaseApiController
    {
        [HttpPost]
        public Result Add(MettingRoom model)
        {
            if ((int)db.Insert(model) > 0)
            {
                return JsonHelper.Success();
            }
            return JsonHelper.Error();
        }
        [HttpPost]
        public Result DeleteList()
        {
            var id = HttpContext.Current.Request.Form["id"];
            if (id == null) return JsonHelper.Success();

            var list = id.Split(',').ToList();
            list.Add("0");
            db.Delete<MettingRoom>("where id in (@0)", list);
            return JsonHelper.Success();
        }
        [HttpPost]
        public Result Update(MettingRoom model)
        {
            if (db.Update(model) > 0)
            {
                return JsonHelper.Success();
            }
            return JsonHelper.Error();
        }

        /// <summary>
        /// 返回首页的相关的房间使用情况
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public Result GetList(GetListQuery model)
        {
            var mettingRoomList = db.Query<MettingRoom>("select a.* ,b.Name MettingName,b.StartDate,b.EndDate from MettingRoom a left join Metting b on a.Id=b.MettingRoomId                 left join Employee c on c.userId=@0 where b.MettingRoomId is not null and b.startDate>@1 and departmentId", model.userId, model.day);


            return JsonHelper.Success();
        }
    }

    public class GetListQuery
    {
        public DateTime day { get; set; }
        public int userId { get; set; }
    }

    public class GetListResult
    {
        public string Address { get; set; }
        public List<TimeResult> Results { get; set; }

    }
    public class TimeResult
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }
}